package com.anan.builder;

import com.anan.bean.FieldInfo;
import com.anan.bean.TableInfo;
import com.anan.utils.StringTools;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class BuildEntity {

    static String projectName = "";

    public static void create(TableInfo tableInfo, String projectName) {
        BuildEntity.projectName = projectName;
        createPo(tableInfo);
        createVo(tableInfo);
        createQuery(tableInfo);
        createDto(tableInfo);
    }

    // TableInfo{tableName='liabilities', beanName='Liabilities', comment='负债表',
    // fieldList=[FieldInfo{FieldName='id', propertyName='id', sqlType='varchar', javaType='String', comment='ID', isAutoIncrement=false}, FieldInfo{FieldName='user_id', propertyName='userId', sqlType='varchar', javaType='String', comment='用户id', isAutoIncrement=false}, FieldInfo{FieldName='time', propertyName='time', sqlType='varchar', javaType='String', comment='时间', isAutoIncrement=false}, FieldInfo{FieldName='name', propertyName='name', sqlType='varchar', javaType='String', comment='负债名称', isAutoIncrement=false}, FieldInfo{FieldName='quantity', propertyName='quantity', sqlType='double', javaType='Double', comment='金额', isAutoIncrement=false}, FieldInfo{FieldName='notes', propertyName='notes', sqlType='varchar', javaType='String', comment='备注', isAutoIncrement=false}, FieldInfo{FieldName='state', propertyName='state', sqlType='int', javaType='Integer', comment='1正常 0停用', isAutoIncrement=false}], keyIndexMap={PRIMARY=[FieldInfo{FieldName='id', propertyName='id', sqlType='varchar', javaType='String', comment='ID', isAutoIncrement=false}]}, haveDate=null, haveDateTime=null, haveBigDecimal=true}
    public static void createPo(TableInfo tableInfo) {
        String path = System.getProperty("user.dir")+ "/" + projectName + "/src/main/java/com/anan/entity/po";
        String beanName = tableInfo.getBeanName();
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, beanName + ".java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;

        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.entity.po;\n\n");
            bw.write("import java.io.Serializable;\n\n\n");
            bw.write("// " + tableInfo.getComment() + "\n");
            bw.write("public class " + beanName + " implements Serializable {\n\n");
            // 编写属性
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                bw.write("\tprivate " + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ";  // " + fieldInfo.getComment() + "\n");
            }
            // 编写get set 方法
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                bw.write("\tpublic " + fieldInfo.getJavaType() + " get" + StringTools.upperCaseFirstLetter(fieldInfo.getPropertyName()) + "() {\n");
                bw.write("\t\treturn this." + fieldInfo.getPropertyName() + ";\n\t}\n\n");

                bw.write("\tpublic void set" + StringTools.upperCaseFirstLetter(fieldInfo.getPropertyName()) + "(" + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ") {\n");
                bw.write("\t\tthis." + fieldInfo.getPropertyName() + " = " + fieldInfo.getPropertyName() + ";\n\t}\n\n");

            }
            bw.write("\t@Override\n\tpublic String toString() {\n\t\treturn \"" + beanName + "{\" +\n");
            boolean bool = true;
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                if (bool) {
                    if ("String".equals(fieldInfo.getJavaType())) {
                        bw.write("\t\t\t\t\"" + fieldInfo.getPropertyName() + "='\" + " + fieldInfo.getPropertyName() + " + '\\'' +\n");
                    } else {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "=\" + " + fieldInfo.getPropertyName() + " +\n");
                    }
                } else {
                    if ("String".equals(fieldInfo.getJavaType())) {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "='\" + " + fieldInfo.getPropertyName() + " + '\\'' +\n");
                    } else {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "=\" + " + fieldInfo.getPropertyName() + " +\n");
                    }
                }
                bool = false;
            }
            bw.write("\t\t\t\t'}';\n\t}\n}");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    public static void createVo(TableInfo tableInfo) {
        String path = System.getProperty("user.dir")+ "/" + projectName + "/src/main/java/com/anan/entity/vo";
        String beanName = tableInfo.getBeanName() + "Vo";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, beanName + ".java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;

        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.entity.vo;\n\n");
            bw.write("import java.io.Serializable;\n\n\n");
            bw.write("// " + tableInfo.getComment() + "\n");
            bw.write("public class " + beanName + " implements Serializable {\n\n");
            // 编写属性
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                bw.write("\tprivate " + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ";  // " + fieldInfo.getComment() + "\n");
            }
            // 编写get set 方法
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                bw.write("\tpublic " + fieldInfo.getJavaType() + " get" + StringTools.upperCaseFirstLetter(fieldInfo.getPropertyName()) + "() {\n");
                bw.write("\t\treturn this." + fieldInfo.getPropertyName() + ";\n\t}\n\n");

                bw.write("\tpublic void set" + StringTools.upperCaseFirstLetter(fieldInfo.getPropertyName()) + "(" + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ") {\n");
                bw.write("\t\tthis." + fieldInfo.getPropertyName() + " = " + fieldInfo.getPropertyName() + ";\n\t}\n\n");

            }
            bw.write("\t@Override\n\tpublic String toString() {\n\t\treturn \"" + beanName + "{\" +\n");
            boolean bool = true;
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                if (bool) {
                    if ("String".equals(fieldInfo.getJavaType())) {
                        bw.write("\t\t\t\t\"" + fieldInfo.getPropertyName() + "='\" + " + fieldInfo.getPropertyName() + " + '\\'' +\n");
                    } else {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "=\" + " + fieldInfo.getPropertyName() + " +\n");
                    }
                } else {
                    if ("String".equals(fieldInfo.getJavaType())) {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "='\" + " + fieldInfo.getPropertyName() + " + '\\'' +\n");
                    } else {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "=\" + " + fieldInfo.getPropertyName() + " +\n");
                    }
                }
                bool = false;
            }
            bw.write("\t\t\t\t'}';\n\t}\n}");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    public static void createQuery(TableInfo tableInfo) {
        String path = System.getProperty("user.dir")+ "/" + projectName + "/src/main/java/com/anan/entity/query";
        String beanName = tableInfo.getBeanName() + "Query";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, beanName + ".java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;

        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.entity.query;\n\n");
            bw.write("import java.io.Serializable;\n\n\n");
            bw.write("// " + tableInfo.getComment() + "\n");
            bw.write("public class " + beanName + " implements Serializable {\n\n");
            // 编写属性
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                bw.write("\tprivate " + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ";  // " + fieldInfo.getComment() + "\n");
            }
            // 编写get set 方法
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                bw.write("\tpublic " + fieldInfo.getJavaType() + " get" + StringTools.upperCaseFirstLetter(fieldInfo.getPropertyName()) + "() {\n");
                bw.write("\t\treturn this." + fieldInfo.getPropertyName() + ";\n\t}\n\n");

                bw.write("\tpublic void set" + StringTools.upperCaseFirstLetter(fieldInfo.getPropertyName()) + "(" + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ") {\n");
                bw.write("\t\tthis." + fieldInfo.getPropertyName() + " = " + fieldInfo.getPropertyName() + ";\n\t}\n\n");

            }
            bw.write("\t@Override\n\tpublic String toString() {\n\t\treturn \"" + beanName + "{\" +\n");
            boolean bool = true;
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                if (bool) {
                    if ("String".equals(fieldInfo.getJavaType())) {
                        bw.write("\t\t\t\t\"" + fieldInfo.getPropertyName() + "='\" + " + fieldInfo.getPropertyName() + " + '\\'' +\n");
                    } else {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "=\" + " + fieldInfo.getPropertyName() + " +\n");
                    }
                } else {
                    if ("String".equals(fieldInfo.getJavaType())) {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "='\" + " + fieldInfo.getPropertyName() + " + '\\'' +\n");
                    } else {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "=\" + " + fieldInfo.getPropertyName() + " +\n");
                    }
                }
                bool = false;
            }
            bw.write("\t\t\t\t'}';\n\t}\n}");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    public static void createDto(TableInfo tableInfo) {
        String path = System.getProperty("user.dir")+ "/" + projectName + "/src/main/java/com/anan/entity/dto";
        String beanName = tableInfo.getBeanName() + "Dto";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, beanName + ".java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;

        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.entity.dto;\n\n");
            bw.write("import java.io.Serializable;\n\n\n");
            bw.write("// " + tableInfo.getComment() + "\n");
            bw.write("public class " + beanName + " implements Serializable {\n\n");
            // 编写属性
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                bw.write("\tprivate " + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ";  // " + fieldInfo.getComment() + "\n");
            }
            // 编写get set 方法
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                bw.write("\tpublic " + fieldInfo.getJavaType() + " get" + StringTools.upperCaseFirstLetter(fieldInfo.getPropertyName()) + "() {\n");
                bw.write("\t\treturn this." + fieldInfo.getPropertyName() + ";\n\t}\n\n");

                bw.write("\tpublic void set" + StringTools.upperCaseFirstLetter(fieldInfo.getPropertyName()) + "(" + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ") {\n");
                bw.write("\t\tthis." + fieldInfo.getPropertyName() + " = " + fieldInfo.getPropertyName() + ";\n\t}\n\n");

            }
            bw.write("\t@Override\n\tpublic String toString() {\n\t\treturn \"" + beanName + "{\" +\n");
            boolean bool = true;
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                if (bool) {
                    if ("String".equals(fieldInfo.getJavaType())) {
                        bw.write("\t\t\t\t\"" + fieldInfo.getPropertyName() + "='\" + " + fieldInfo.getPropertyName() + " + '\\'' +\n");
                    } else {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "=\" + " + fieldInfo.getPropertyName() + " +\n");
                    }
                } else {
                    if ("String".equals(fieldInfo.getJavaType())) {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "='\" + " + fieldInfo.getPropertyName() + " + '\\'' +\n");
                    } else {
                        bw.write("\t\t\t\t\", " + fieldInfo.getPropertyName() + "=\" + " + fieldInfo.getPropertyName() + " +\n");
                    }
                }
                bool = false;
            }
            bw.write("\t\t\t\t'}';\n\t}\n}");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

}
